//
//  ChangeWordController.m
//  DAKnowledge
//
//  Created by DreamArts on 16/5/26.
//  Copyright © 2016年 lidongyang. All rights reserved.
//

#import "DAChangeWordController.h"
#import "SVProgressHUD.h"
#import "DAloginPersonalInformation.h"
@interface DAChangeWordController()<UITextFieldDelegate>{
    UITextField *textField;
}
@property (nonatomic, strong) DAloginPersonalInformation *loginPersonalInformation;

@end

@implementation DAChangeWordController
- (void)viewDidLoad{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:backGroundColor];
    textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 20, SCREENWIDTH, 44)];
    textField.backgroundColor = [UIColor whiteColor];
    textField.font = [UIFont systemFontOfSize:14];
    textField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    textField.layer.borderWidth = 0.5;
    UIView *holdView = [[UIView alloc]initWithFrame:CGRectMake(2, 0, 10, 10)];
    textField.leftView = holdView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    if (![self.oldString isKindOfClass:[NSNull class]]) {
        textField.text = self.oldString;
    }
    [self.view addSubview:textField];
    
    NSString *change = NSLocalizedString(@"change", nil);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:change style:UIBarButtonItemStyleDone target:self action:@selector(onTouchModify)];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dissmissKeyboard)];
    [self.view addGestureRecognizer:tap];
}

- (void)dissmissKeyboard{
    [textField resignFirstResponder];
}

- (void)onTouchModify{
    [textField resignFirstResponder];
    NSString *URL= DAChangeUserInfo;
    NSDictionary *dic;
    NSString *isNotEmpty = NSLocalizedString(@"userNameCanNotBeEmpty", nil);
    if(self.changeWordType == ChangeWordTypeName){
        dic = @{@"userID": self.userID,
                @"userName": textField.text,
                @"userSign": self.anotherString};
    }else if(self.changeWordType == ChangeWordTypeSign) {
        dic = @{@"userID": self.userID,
                @"userSign": textField.text,
                @"userName": self.anotherString};
    }
    [SVProgressHUD show];
    [DADataHandel postURL:URL parame:dic cookie:nil result:^(id result) {
        if (result) {
            if([result[@"result"]isEqualToString:@"true"]){
                [SVProgressHUD dismiss];
                [self.navigationController popViewControllerAnimated:YES];
                [self.delegate changeWordFinished:dic];
                [self userInfoArchiveLocal];
            }else{
                NSLog(@"数据修改失败。");
                if (self.changeWordType == ChangeWordTypeName && textField.text.length == 0) {
                    [SVProgressHUD showErrorWithStatus:isNotEmpty];
                }else{
                    [SVProgressHUD dismiss];
                }
                
            }
        } else {
            NSLog(@"onTouchModify：数据异常。");
        }
       // [SVProgressHUD dismiss];
    }];
}
#pragma mark - 修改成功后 把个人信息再存到本地
- (void)userInfoArchiveLocal{
    /** 解档*/
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES).lastObject stringByAppendingString:@"DALogin"];
    self.loginPersonalInformation = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    if (self.changeWordType == ChangeWordTypeName) {
        self.loginPersonalInformation.userName = textField.text;
    }else{
        self.loginPersonalInformation.userSign = textField.text;
    }
    
    // 再存到本地
    [NSKeyedArchiver archiveRootObject:self.loginPersonalInformation toFile:filePath];
    
}


@end
